// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PlanAction.java

package com.iphaeton.lms.action.plan;

import com.iphaeton.common.helper.*;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.organize.IOrganizeService;
import com.iphaeton.lms.business.plan.IPlanService;
import com.iphaeton.lms.pojo.plan.Plan;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;

// Referenced classes of package com.iphaeton.lms.action.plan:
//            PlanBizSupportAction

public class PlanAction extends PlanBizSupportAction
{

    public PlanAction()
    {
    }

    public ActionForward setPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        java.util.List list = null;
        try
        {
            UserSession us = (UserSession)request.getSession().getAttribute("userSession");
            String orgId = us.getManageOrgId();
            list = organize.findAllChildOrganize(organize.findOrganizeById(orgId));
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("faiure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", list);
        return mapping.findForward("plan_create");
    }

    public ActionForward createPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = df.format(time);
        logger.info("\u5F53\u524D\u65F6\u95F4:" + currentTime);
        daf = (DynaActionForm)form;
        String sn = (String)daf.get("sn");
        String name = (String)daf.get("name");
        String comment = (String)daf.get("comment");
        Integer state = (Integer)daf.get("state");
        FormFile file = (FormFile)daf.get("file");
        String organizeInfo = (String)daf.get("organizeId");
        String organizeId = organizeInfo.substring(0, organizeInfo.indexOf(38));
        String organizeUrl = organizeInfo.substring(organizeInfo.indexOf(38) + 1, organizeInfo.length());
        Plan p = new Plan();
        p.setSn(sn);
        p.setName(name);
        p.setComment(comment);
        p.setOrganizeId(organizeId);
        p.setOrganizeUrl(organizeUrl);
        p.setState(state);
        p.setDeleted(new Integer(1));
        try
        {
            UserSession us = (UserSession)request.getSession().getAttribute("userSession");
            String createrId = us.getId();
            String createrUid = us.getUserId();
            String createrName = us.getUserName();
            p.setCreaterId(createrId);
            p.setCreaterUid(createrUid);
            p.setCreaterName(createrName);
            p = plan.createPlan(p);
            if(file != null && !file.getFileName().equals(""))
            {
                upload.upload(file, request.getRealPath("/") + "upload" + File.separator + "plan" + File.separator + p.getId() + File.separator);
                String tag = file.getFileName();
                p.setTag(tag);
                plan.createPlan(p);
            }
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("plan", p);
        return mapping.findForward("plan_create_ok");
    }

    public ActionForward updatePlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String sn = (String)daf.get("sn");
        String id = (String)daf.get("id");
        String name = (String)daf.get("name");
        String comment = (String)daf.get("comment");
        Integer state = (Integer)daf.get("state");
        FormFile file = (FormFile)daf.get("file");
        String organizeInfo = (String)daf.get("organizeId");
        String organizeId = organizeInfo.substring(0, organizeInfo.indexOf(38));
        String organizeUrl = organizeInfo.substring(organizeInfo.indexOf(38) + 1, organizeInfo.length());
        Plan p;
        try
        {
            p = plan.findPlanById(id);
            p.setSn(sn);
            p.setName(name);
            p.setComment(comment);
            p.setOrganizeId(organizeId);
            p.setOrganizeUrl(organizeUrl);
            p.setState(state);
            p.setDeleted(new Integer(1));
            if(file != null && !file.getFileName().equals(""))
            {
                upload.upload(file, request.getRealPath("/") + "upload" + File.separator + "plan" + File.separator + p.getId() + File.separator);
                String tag = file.getFileName();
                p.setTag(tag);
                plan.createPlan(p);
            }
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("plan", p);
        return mapping.findForward("plan_edit_ok");
    }

    public ActionForward selectPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        Integer state = (Integer)daf.get("state");
        if(state == null)
            state = new Integer(0);
        int currentPage = 0;
        int pageSize = 10;
        String page = request.getParameter("page");
        String row = request.getParameter("row");
        if(row == null)
            row = "";
        java.util.List list = null;
        try
        {
            UserSession us = (UserSession)request.getSession().getAttribute("userSession");
            String userId = us.getId();
            if(page == null || "".equals(page))
                currentPage = 1;
            else
                currentPage = Integer.parseInt(page);
            pageInfo = plan.selectPlan(userId, state, row, pageSize);
            pageInfo.setPage(currentPage);
            pageInfo.setRow(row);
            request.getSession().setAttribute("pageInfo", pageInfo);
            list = plan.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("state", state);
        request.setAttribute("list", list);
        return mapping.findForward("plan_list");
    }

    public ActionForward releasePlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String planId = request.getParameter("planId");
        Plan p = null;
        GregorianCalendar gc = new GregorianCalendar();
        String currentTime = gc.get(1) + "-" + (gc.get(2) + 1) + "-" + gc.get(5);
        try
        {
            p = plan.findPlanById(planId);
            p.setReleaseDate(currentTime);
            plan.createPlan(p);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("planList");
    }

    public ActionForward editPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String planId = request.getParameter("planId");
        Plan p = null;
        java.util.List list = null;
        try
        {
            UserSession us = (UserSession)request.getSession().getAttribute("userSession");
            String orgId = us.getOrgId();
            list = organize.findAllChildOrganize(organize.findOrganizeById(orgId));
            p = plan.findPlanById(planId);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", list);
        request.setAttribute("plan", p);
        return mapping.findForward("plan_edit");
    }

    public ActionForward removePlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String planId[] = id.split("&");
        try
        {
            for(int i = 0; i < planId.length; i++)
            {
                Plan p = plan.findPlanById(planId[i]);
                plan.removePlan(p);
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("planList");
    }

    public ActionForward deletePlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String planId[] = id.split("&");
        try
        {
            for(int i = 0; i < planId.length; i++)
            {
                Plan p = plan.findPlanById(planId[i]);
                plan.deletePlan(planId[i]);
                String tag = p.getTag();
                if(tag != null && !"".equals(tag))
                    upload.remove(request.getRealPath("/") + "upload" + File.separator + "plan" + File.separator + p.getId() + File.separator + p.getTag());
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("planList");
    }

    public ActionForward planDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String planId = request.getParameter("id");
        Plan p = null;
        try
        {
            p = plan.findPlanById(planId);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("plan", p);
        return mapping.findForward("plan_detail");
    }

    public ActionForward download(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        Plan p;
        try
        {
            p = plan.findPlanById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        String tag = p.getTag();
        request.setAttribute("url", request.getRealPath("/") + "upload" + File.separator + "plan" + File.separator + p.getId() + File.separator + tag);
        request.setAttribute("name", tag);
        return mapping.findForward("download");
    }

    public ActionForward studentPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        int currentPage = 0;
        int pageSize = 10;
        String page = request.getParameter("page");
        String row = request.getParameter("row");
        if(row == null)
            row = "";
        java.util.List list = null;
        try
        {
            UserSession us = (UserSession)request.getSession().getAttribute("userSession");
            String org_url = us.getOrgUrl();
            if(page == null || "".equals(page))
                currentPage = 1;
            else
                currentPage = Integer.parseInt(page);
            pageInfo = plan.studentPlan(org_url, row, pageSize);
            pageInfo.setPage(currentPage);
            pageInfo.setRow(row);
            request.getSession().setAttribute("pageInfo", pageInfo);
            list = plan.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", list);
        return mapping.findForward("plan_view");
    }

    private static Logger logger;
    private DynaActionForm daf;
    private Pagination pageInfo;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.plan.PlanAction.class.getName());
    }
}
